package com.excel.validators.core.extension;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.excel.validators.log.Log;
import com.excel.validators.log.LogFactory;

/**
 * @author wangxiaoliang10
 * 
 * 2017年8月5日
 */
public class TypeConverter {

		protected static final Log logger = LogFactory.getLog(TypeConverter.class);
	
    public static String toString(String value) {
        if (value == null) return null;
        return String.valueOf(value);
    }

    public static Integer toInteger(String value) {
        if (value == null) return null;
        double d = toDouble(value,null);
        return Integer.valueOf((int)d);
    }

    public static Long toLong(String value){
        if (value == null) return null;
        double d = toDouble(value,null);
        return Long.valueOf((long)d);
    }

    public static Double toDouble(String value, Integer decimal) {
        if (value == null) return null;
        if (decimal != null) {
        		return toBigDecimal(value,decimal).doubleValue();
        }
        return Double.valueOf(value);
    }

    public static Float toFloat(String value, Integer decimal) {
    			if (value == null) return null;
    			if (decimal != null) {
    					return toBigDecimal(value,decimal).floatValue();
    			}
        return Float.valueOf(value);
    }

    public static BigDecimal toBigDecimal(String value, Integer decimal) {
        if (value == null) return null;
        Integer _decimal = decimal == null ? 2 : decimal;
        return new BigDecimal(value).setScale(_decimal, RoundingMode.HALF_UP);
    }

    public static Date toDate(String value,String pattern) {
					if (value == null) return null;
					return dateParse(value,pattern);
    }

    public static Boolean toBoolean(String value){
        if (value == null) return null;
        return Boolean.valueOf(value);
    }

		private static final ThreadLocal<SimpleDateFormat> dataFormat = new ThreadLocal<SimpleDateFormat>() {
				@Override
				public SimpleDateFormat get() {
						SimpleDateFormat format = super.get();
						if (format == null) {
								format = new SimpleDateFormat();
								set(format);
						}
						return format;
				}
		};

		/**
		 * 解析excel中的时间
		 * @param date
		 * @return
		 * @throws ParseException 
		 */
		public static Date dateParse(String date,String pattern) {
				SimpleDateFormat dateFormat = dataFormat.get();
				dateFormat.applyPattern(pattern);
				try {
						return dateFormat.parse(date);
				} catch (Exception e) {
						logger.error("Unsupported date type: " + date,e);
				}
				return null;
		}

		/**
		 * 解析excel中的时间
		 * @param date
		 * @return
		 * @throws ParseException 
		 */
		public static String formatDate(Date date,String pattern) {
				if (date == null) return null;
				
				SimpleDateFormat dateFormat = dataFormat.get();
				dateFormat.applyPattern(pattern);
				try {
						return dateFormat.format(date);
				} catch (Exception e) {
						logger.error("Unsupported date type: " + date.toString(),e);
				}
				return null;
		}

		public static Byte toByte(String value) {
				if (value == null) return null;
				return Byte.valueOf(value);
		}

		public static Short toShort(String value) {
				if (value == null) return null;
				return Short.valueOf(value);
		}

		public static Character toChar(String value) {
				if (value == null || value.length() == 0) return null;
				return value.charAt(0);
		}
		
}
